package com.example.avmanagerweb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.avmanagerweb.entity.Docqry;
import com.example.avmanagerweb.entity.vo.FileCondition;
import com.example.avmanagerweb.result.RLIST;
import com.example.avmanagerweb.service.DocqryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2023-08-16
 */
@RestController
@RequestMapping("/docqry")
public class DocqryController {

    @Autowired
    private DocqryService docqryService;

    @RequestMapping(value = "/docData", method = RequestMethod.POST)
    public RLIST docDataList(@RequestBody FileCondition fileCondition, HttpServletRequest request ) {
        System.out.println("123");
        //设备id
        String devId = fileCondition.getDevId();
        System.out.println(devId+"devId");
        //警员名
        String uName = fileCondition.getName();
        System.out.println(uName+"uName");
        //警员编号
        String uLoginName = fileCondition.getLoginName();
        System.out.println(uLoginName+"uLoginName");
        //记录仪序号
        String dvNum = fileCondition.getDvNum();
        System.out.println(dvNum+"dvNum");
        //开始时间
        String begin = fileCondition.getBegin();
        System.out.println(begin+"dShootingTime");
        //结束时间
        String end = fileCondition.getEnd();
        System.out.println(end+"dShootingTime   end");
        //文件名
        String fileName = fileCondition.getFileName();
        //文件类型
        String fileType = fileCondition.getFileType();
        //获取当前时间
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式(年-月-日-时-分-秒)
        String createTime = dateFormat.format(now);//格式化然后放入字符串中
        //条件
        QueryWrapper wrapper = new QueryWrapper();

        //设备id条件判断
        if(!StringUtils.isEmpty(devId)){
            wrapper.eq("ddvId",devId);
        }
        //警员名
        if(!StringUtils.isEmpty(uName)){
            wrapper.like("uName",uName);
        }
        //警员编号
        if(!StringUtils.isEmpty(uLoginName)){
            wrapper.eq("uLoginName",uLoginName);
        }
        //记录仪序号
        if(!StringUtils.isEmpty(dvNum)){
            wrapper.like("dvNum",dvNum);
        }

        if(!StringUtils.isEmpty(fileType)){
            wrapper.eq("dDtID",fileType);
        }
        if(!StringUtils.isEmpty(fileName)){
            wrapper.like("dFilePath",fileName);
        }
        if( fileCondition.getTimeType() == 0 ){
            //拍摄开始时间
            if(!StringUtils.isEmpty(begin)){
                //构建条件
                wrapper.ge("dShootingTime",begin);

            } else {
                String begin2 = createTime+" 00:00:00.000";
                wrapper.ge("dShootingTime",begin2);

            }
            //拍摄结束时间
            if(!StringUtils.isEmpty(end)){
                //构建条件
                wrapper.le("dShootingTime",end);
            } else {
                String end2 = createTime+" 23:59:59.999";
                wrapper.le("dShootingTime",end2);

            }
        } else if ( fileCondition.getTimeType() == 1 ){
            //上传开始时间
            if(!StringUtils.isEmpty(begin)){
                wrapper.ge("dUploadTime",begin);
            } else {
                String begin2 = createTime+" 00:00:00.000";
                wrapper.ge("dUploadTime",begin2);

            }
            //上传结束时间
            if(!StringUtils.isEmpty(end)){
                wrapper.le("dUploadTime",end);
            } else {
                String end2 = createTime+" 23:59:59.999";
                wrapper.le("dUploadTime",end2);

            }
        }


        Page<Docqry> page = new Page<>(fileCondition.getPageNo(),fileCondition.getPageSize());
        wrapper.orderByDesc("dUploadTime");
        docqryService.page(page, wrapper);

        long total = page.getTotal();
        List<Docqry> list = page.getRecords();

        List<String> listIp = new ArrayList<>();
        //ip
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress address = addresses.nextElement();
                    if (address.isSiteLocalAddress() && !address.isLoopbackAddress()
                            && address.getHostAddress().indexOf(":") == -1) {
                        System.out.println("本机的IPv4地址是：" + address.getHostAddress());
                        listIp.add(address.getHostAddress());

                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return RLIST.ok().data("ip",listIp).data("data",list).data("total",total);
    }

}

